<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 图片处理类
 * 主要负责用来处理商品图片和图片上传之类的
 */
class Image extends Auth_Controller{

    public function __construct(){
        parent::__construct();
        $this->load->model('goods_mdl');
        $this->load->model('image_mdl');
    }

    public function update(){
        $gid = $this->input->get('gid',TRUE);
        if(empty($gid)){
            show_404();
            exit;
        }

        $this->db->where('id',intval($gid));
        $res = $this->goods_mdl->get_one();
        $data['res'] = $res;

        if(empty($res)){
            show_error('参数错误！');
        }

        //查询图片

        $this->db->where('goods_id',$gid);
        $this->db->order_by('sort DESC,id ASC');
        $data['list'] = $this->image_mdl->select();

        $this->_view('admin/image_update',$data);
    }

    public function test(){
        
        $gid = $this->input->get('gid',TRUE);

        if(empty($gid)){
            show_404();
            exit;
        }

        $this->db->where('id',intval($gid));
        $res = $this->goods_mdl->get_one();
        $data['res'] = $res;

        if(empty($res)){
            show_error('参数错误！');
        }

        $this->load->library('qiniu/Qiniu_PutExtra');
        $this->load->library('qiniu/Qiniu_RS_GetPolicy');

         $bucket = "dickfanst"; //你的七牛云存储的空间名称

        $accessKey = 'JyG-oAoRklZTirOLuazxEmh-VZCZzuQauW1NwhP6'; //公钥

        $secretKey = '5lAA9auqO1KIWZqUjUFHMyoUF_bvr-mYdH0NYrPn'; //密钥

        Qiniu_SetKeys($accessKey, $secretKey);

        $putPolicy = new Qiniu_RS_PutPolicy($bucket);

        $data['upToken'] = $putPolicy->Token(null);

        //查询图片

        $this->db->where('goods_id',$gid);
        $this->db->order_by('sort DESC,id ASC');
        $data['list'] = $this->image_mdl->select();

        $this->_view('admin/image_test',$data); 
    }


    /**
     * 云存储专用 菜单拉取
     */
    public function get_menu(){
        $gid = $this->input->get('gid',TRUE);
        $data['gid'] = $gid = intval($gid);
        if(empty($gid)){
            die('参数错误！');
        }

        //查询图片

        $this->db->where('goods_id',$gid);
        $this->db->order_by('sort DESC,id ASC');
        $data['list'] = $this->image_mdl->select();

        $this->load->view('admin/widget/image_menu',$data);

    }
    
    /**
     * 图片删除
     */
    public function del_image(){

        if(!$this->input->is_ajax_request()){
            show_404();
            exit;
        }

        $id = $this->input->post('iid',TRUE);
        $id = intval($id);

        if(empty($id)){
            die('参数错误！');
        }

        $this->db->where('id',$id);
        $res = $this->image_mdl->get_one();
        if(empty($res)){
            die('参数错误！');
        }

        $res['path'] = '.'.$res['path'];

        //判断文件是否存在
        if(file_exists($res['path'].$res['file_name'])){
            @unlink($res['path'].$res['file_name']);
        }
        if(file_exists($res['path'].$res['file_name'])){
            @unlink($res['path'].$res['thumb']);
        }
        //删除数据
        $this->db->where('id',$id);
        $res = $this->image_mdl->delete();
        $this->logs->add($res,'删除图片');
        echo $res === true ? '1' : '服务器处理失败！';
    }

    /**
     * 设置用户排序
     */
    public function set_sort(){
        if(!$this->input->is_ajax_request()){
            show_404();
            exit;
        }

        $iid = $this->input->post('iid',TRUE);
        $id = intval($iid);

        if(empty($id)){
            die('参数错误！');
        }

        $val = $this->input->post('val',TRUE);
        $val = intval($val);
        
        $this->db->where('id',$id);
        $res = $this->image_mdl->update(['sort'=>$val]);
        $this->logs->add($res,'修改商品图片排序');

        echo $res === TRUE ? '1' : '修改排序失败！';
    }

}